package week.six;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;


public class Cube {

	private static float unit = 200;
	
	private static float[][] vertices = new float[][] {
		new float [] {
			 unit, unit,-unit,
			-unit, unit,-unit,
			-unit, unit, unit,
			 unit, unit, unit
		},
		new float [] {
			 unit,-unit, unit,
			-unit,-unit, unit,
			-unit,-unit,-unit,
			 unit,-unit,-unit		
		},
		new float [] {
			 unit, unit, unit,
			-unit, unit, unit,
			-unit,-unit, unit,
			 unit,-unit, unit
		},
		new float [] {
			 unit,-unit,-unit,
			-unit,-unit,-unit,
			-unit, unit,-unit,
			 unit, unit,-unit
		},
		new float [] {
			-unit, unit, unit,
			-unit, unit,-unit,
			-unit,-unit,-unit,
			-unit,-unit, unit
		},
		new float [] {
			 1, 1,-1,
			 1, 1, 1,
			 1,-1, 1,
			 1,-1,-1	
		},
		
	};
	
	private static float[] colours = new float[] {
		0,	1,	0,	1,
		1,	0.5f,	0,	1,
		1,	0,	0,	1,
		1,	1,	0,	1,
		0,	0,	1,	1,
		1,	0,	1,	1	
	};
	
	private static FloatBuffer[] vertexBuffer;
//	private static FloatBuffer colourBuffer;
	
//	private static float rotation;
	
	public Cube() {
		ByteBuffer bb;
		vertexBuffer = new FloatBuffer[6];
		for (int i=0; i<6; ++i) {
			bb = ByteBuffer.allocateDirect(vertices[0].length * 4);
			bb.order(ByteOrder.nativeOrder());
			vertexBuffer[i] = bb.asFloatBuffer();
			vertexBuffer[i].put(vertices[i]);
			vertexBuffer[i].position(0);
		}
	}
	
	public void draw(GL10 gl) {
		for (int i=0; i<6; ++i) {
			gl.glColor4f(colours[4*i+0], colours[4*i+1], colours[4*i+2], colours[4*i+3]);
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer[i]);
			gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		}
	}
}
